﻿using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Epam.PeopleModel;

namespace PeopleListTask.Validators
{
    public static class PeopleValidators
    {
        public static void ValidateName(PeopleEntity entity, ModelBindingContext bindingContext)
        {
            try
            {
                Validator.ValidateProperty(entity.Name,
                                           new ValidationContext(entity, null, null) { MemberName = "Name" });
            }
            catch (ValidationException e)
            {
                bindingContext.ModelState.AddModelError("Name", e);
            }
        }

        public static void ValidateDate(PeopleEntity entity, ModelBindingContext bindingContext)
        {
            try
            {
                Validator.ValidateProperty(entity.BirthDate,
                                           new ValidationContext(entity, null, null) { MemberName = "BirthDate" });
            }
            catch (ValidationException e)
            {
                bindingContext.ModelState.AddModelError("BirthDate", e);
            }
        }

        public static void ValidateWeight(PeopleEntity entity, ModelBindingContext bindingContext)
        {
            try
            {
                Validator.ValidateProperty(entity.Weight,
                                           new ValidationContext(entity, null, null) { MemberName = "Weight" });
            }
            catch (ValidationException e)
            {
                bindingContext.ModelState.AddModelError("Weight", e);
            }
        }

        public static void ValidateHeight(PeopleEntity entity, ModelBindingContext bindingContext)
        {
            try
            {
                Validator.ValidateProperty(entity.Height,
                                           new ValidationContext(entity, null, null) { MemberName = "Height" });
            }
            catch (ValidationException e)
            {
                bindingContext.ModelState.AddModelError("Height", e);
            }
        }
    }
}